﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Workflow.ComponentModel;
using Microsoft.SharePoint.WorkflowActions;
using System.Workflow.ComponentModel.Compiler;
using Microsoft.SharePoint;
using System.Globalization;
using System.Web;
using Microsoft.SharePoint.Workflow;

namespace Learn.SharePoint.com
{
    public class RunWorkflowOnItemsActivity : Activity
    {
        public static DependencyProperty __ContextProperty =
            DependencyProperty.Register("__Context", typeof(WorkflowContext), typeof(RunWorkflowOnItemsActivity));
        public static DependencyProperty SiteUrlProperty =
            DependencyProperty.Register("SiteUrl", typeof(String), typeof(RunWorkflowOnItemsActivity));
        public static DependencyProperty ListNameProperty =
            DependencyProperty.Register("ListName", typeof(String), typeof(RunWorkflowOnItemsActivity));
        public static DependencyProperty WorkflowNameProperty =
            DependencyProperty.Register("WorkflowName", typeof(String), typeof(RunWorkflowOnItemsActivity));
        public static DependencyProperty CamlQueryProperty =
            DependencyProperty.Register("CamlQuery", typeof(String), typeof(RunWorkflowOnItemsActivity));
        public static DependencyProperty InitiationParametersProperty =
            DependencyProperty.Register("InitiationParameters", typeof(String), typeof(RunWorkflowOnItemsActivity));

        [ValidationOption(ValidationOption.Optional)]
        public WorkflowContext __Context
        {
            get
            {
                return (WorkflowContext)base.GetValue(__ContextProperty);
            }
            set
            {
                base.SetValue(__ContextProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public String SiteUrl
        {
            get
            {
                return (String)base.GetValue(SiteUrlProperty);
            }
            set
            {
                base.SetValue(SiteUrlProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public String ListName
        {
            get
            {
                return (String)base.GetValue(ListNameProperty);
            }
            set
            {
                base.SetValue(ListNameProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public String WorkflowName
        {
            get
            {
                return (String)base.GetValue(WorkflowNameProperty);
            }
            set
            {
                base.SetValue(WorkflowNameProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public String CamlQuery
        {
            get
            {
                return (String)base.GetValue(CamlQueryProperty);
            }
            set
            {
                base.SetValue(CamlQueryProperty, value);
            }
        }

        [ValidationOption(ValidationOption.Optional)]
        public String InitiationParameters
        {
            get
            {
                return (String)base.GetValue(InitiationParametersProperty);
            }
            set
            {
                base.SetValue(InitiationParametersProperty, value);
            }
        }

        protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
        {
            try
            {
                using (var site = new SPSite(SiteUrl))
                {
                    using (var web = site.OpenWeb())
                    {
                        var list = web.Lists[ListName];
                        var workflowAssociation = list.WorkflowAssociations.GetAssociationByName(WorkflowName, CultureInfo.CurrentCulture);
                        var query = new SPQuery();
                        query.Query = CamlQuery;
                        var items = list.GetItems(query);
                        foreach (SPListItem item in items)
                        {
                            site.WorkflowManager.StartWorkflow(item,
                                workflowAssociation,
                                InitiationParameters);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                try
                {
                    SPWorkflow.CreateHistoryEvent(__Context.Web, __Context.WorkflowInstanceId, 0, __Context.Web.CurrentUser, TimeSpan.MinValue, "", ex.ToString(), "");
                }
                catch (Exception)
                {                                        
                }
            }
            
            return base.Execute(executionContext);
        }
    }
}